package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Customer;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.service.ICustomerService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.CustomerQueryObject;
import cn.wolfcode.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryService SystemDictionaryService;

    @RequiresPermissions(value = {"customer:potentialList","潜在客户页面"},logical = Logical.OR)
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //设置查询的数据客户状态条件为潜在客户(只查潜在)
        qo.setStatus(Customer.STATUS_COMMON);
        //不是超级管理员或则销售经理只能查询自己的客户
        Subject subject = SecurityUtils.getSubject();
        if(!(subject.hasRole("admin")||subject.hasRole("Market_Manager"))){
            //获取当前登录用户id
            Employee employee = (Employee) subject.getPrincipal();
            //设置当前员工id查询查询条件
            qo.setSellerId(employee.getId());
        }
        //查询销售人员和销售经理,共享给页面做下拉框
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);

        //查询工作的字典明细,共享给页面做下拉框
        List<SystemDictionaryItem> jobs = SystemDictionaryService.queryBySn("job");
        model.addAttribute("jobs",jobs);
        //查询来源的字典明细,共享给页面做下拉框
        List<SystemDictionaryItem> sources = SystemDictionaryService.queryBySn("source");
        model.addAttribute("sources",sources);
        //查询交流方式的字典明细,共享给页面做下拉框
        List<SystemDictionaryItem> ccts = SystemDictionaryService.queryBySn("communicationMethod");
        model.addAttribute("ccts",ccts);

        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/potentialList";
    }

    @RequiresPermissions(value = {"customer:list","客户页面"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/list";
    }

    @RequiresPermissions(value = {"customer:poolList","客户池页面"},logical = Logical.OR)
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        //设置查询的数据客户状态条件为客户池
        qo.setStatus(Customer.STATUS_POOL);
        //查询销售人员和销售经理,共享给页面做下拉框
        List<Employee> sellers = employeeService.selectByRoleSn("Market", "Market_Manager");
        model.addAttribute("sellers",sellers);
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/poolList";
    }

    @RequiresPermissions(value = {"customer:failList","失败客户页面"},logical = Logical.OR)
    @RequestMapping("/failList")
    public String failList(Model model, @ModelAttribute("qo") CustomerQueryObject qo){
        PageInfo<Customer> pageResult = customerService.query(qo);
        model.addAttribute("pageResult", pageResult);
        return "customer/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"customer:delete","潜在客户删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            customerService.delete(id);
        }
        return new JsonResult();
    }

    //修改客户状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Customer customer){
        customerService.changeStatus(customer);
        return new JsonResult();
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"customer:saveOrUpdate","潜在客户新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer){
        String tel = customer.getTel();
        if (tel == null || tel == ""){
            return new JsonResult(false,"手机号必填");
        }
        //查询所有用户的手机号
        HashMap<Long, String> telmap = new HashMap<>();
        List<Customer> customers = customerService.listAll();
        for (Customer customer1 : customers) {
            telmap.put(customer1.getId(),customer1.getTel());
        }

        if (customer.getId() != null) {
            //排除没有修改的情况
            telmap.remove(customer.getId());
            if (telmap.containsValue(tel)){
                return new JsonResult(false,"手机号已存在");
            }
            customerService.update(customer);
        }else {
            if (telmap.containsValue(tel)){
                return new JsonResult(false,"手机号已存在");
            }
            customerService.save(customer);
        }
        return new JsonResult();
    }
}
